// var base64 = require('/node_modules/base-64/base64.js')
var app = angular.module('webApp', ['ui.router', 'oc.lazyLoad', 'ngCookies','iService','iDirective','Filter','ksSwiper'/*,'angularFileUpload'*/]);
// 页面路由
app.config(['$controllerProvider','$stateProvider','$urlRouterProvider', '$locationProvider', '$ocLazyLoadProvider',function($controllerProvider,$stateProvider,$urlRouterProvider,$locationProvider,$ocLazyLoadProvider) {
    app.register = {
        controller: $controllerProvider.register
    };
    $ocLazyLoadProvider.config({
        debug: false
    });
    //路由状态重写
    var loadResolve = function (files,options) {
        var loadPlugin = {
            deps: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load({
                    files: files
                });
            }]
        }
        if(options){
            angular.forEach(options,function (value,key) {
                loadPlugin[key] = value;
            })
        }
        return loadPlugin;
    }
    //动态路由
    $stateProvider
    .state('home', {    //首页
        url:'/',
        templateUrl: 'views/home.html',
        controller:'homeCtrl',
        resolve : loadResolve([__uri('../css/home.less'),__uri('/src/js/ctrl/home.js')])
    }).state('login', {    //登录
        url:'/login',
        templateUrl: 'views/login.html',
        controller:'loginCtrl',
        resolve : loadResolve([__uri('../css/login.less'),__uri('/node_modules/js-md5/src/md5.js'),__uri('/src/js/ctrl/login.js')])
    }).state('register', {    //注册
        url:'/register',
        templateUrl: 'views/register.html',
        controller:'registerCtrl',
        resolve : loadResolve([__uri('../css/register.less'),__uri('/node_modules/js-md5/src/md5.js'),__uri('/src/js/ctrl/register.js')])
    }).state('detail', {    //活动详情
        url:'/detail/:nEventId',
        templateUrl: 'views/detail.html',
        controller:'detailCtrl',
        resolve : loadResolve([__uri('../css/detail.less'),__uri('/src/js/ctrl/detail.js'),__uri('/node_modules/js-md5/src/md5.js')])
    })
       /* .state('enrol', {    //报名
        // url:'/enrol/:nEventId',
        url:'/enrol/:nEventId',
        templateUrl: 'views/enrol.html',
        controller:'onrolCtrl',
        resolve : loadResolve([__uri('/src/js/ctrl/onrol.js'),__uri('../css/onrol.less')],{
            // I will cause a 1 second delay
            delay: ['$q', 'Context', '$location',function($q, Context, $location) {
                var delay = $q.defer();
                console.log(Context.isUserLogin())
                if(!Context.isUserLogin()){
                    $location.path('/login');
                }else{
                    delay.resolve();
                }
                return delay.promise;
            }]
        })
    });*/
    $urlRouterProvider.otherwise('/');
    // $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix('');
}]);

// 在所有route跳转前进行登录检查，目标页面需要登录且用户未登录，则跳转到登录页面
app.run(['$rootScope', 'Context', '$location', function($rootScope,Context,$location) {
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
        /*if(toState.name=='enrol' && !Context.isUserLogin()){
            $location.path('/login');
        }*/
    })
}]);


//公用的方法
app.controller('appCtrl',['$scope','$rootScope','$location','$http','$timeout', function ($scope,$rootScope,$location,$http,$timeout) {
    $scope.config = {
        // host :location.hostname=='120.76.28.144'?'//www.cheerhub.com.hk':'//qa.cheerhub.com.hk',
        // host :location.hostname=='m.cheerhub.com.cn'?'//www.cheerhub.com.hk':'//www.cheerhub.com.hk'',
        // host :location.hostname=='test.cheerhub.com.hk'?'//qa.cheerhub.com.hk':'//qa.cheerhub.com.hk',
        host :location.hostname=='m.cheerhub.com.hk'?'//www.cheerhub.com.hk':'//qa.cheerhub.com.hk',
        // host : '//www.cheerhub.com.hk',
        // host : '//192.168.1.68',
        // host : '//qa.cheerhub.com.hk',
        // detailId : 381
    }

    //向下滑动的事件
    $scope.slidedown = function () {

    }

    //显示出弹框
    $scope.popShow = function (value) {
        $scope.ispopshow = value;
        $timeout(function () {
            $scope.ispopshow = false;
        },2000)
    }
    
    //动态跳转页面
    $scope.goPage = function (uri) {
        // alert(uri);
        $location.path(uri);
    }

    //动态跳转base64页面
    /*$scope.goPageBase64 = function (uri,code) {
        console.log(uri+base64.encode(code));
        $location.path(uri+base64.encode(code));
    }*/

    //设置title
    $scope.update_title = function(title) {
        var body = document.getElementsByTagName('body')[0];
        document.title = title;
        var iframe = document.createElement("iframe");
        iframe.setAttribute("src", "/empty.png");

        iframe.addEventListener('load', function() {
            setTimeout(function() {
                iframe.removeEventListener('load');
                document.body.removeChild(iframe);
            }, 0);
        });
        document.body.appendChild(iframe);
    }
}]);